package edu.sysu.cshare.util.gui;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.JToolTip;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableModel;

import edu.sysu.cshare.control.MessageManager;
import edu.sysu.cshare.gui.Dlg_Msg;
import edu.sysu.cshare.gui.Pnl_Homework;
import edu.sysu.cshare.model.Message;
import edu.sysu.cshare.util.Mis;
import edu.sysu.cshare.util.gui.GUI.MultiLineToolTip;

public class Table_Msg extends JTable {
	private MessageManager messageManager;

	public Table_Msg(TableModel dm, MessageManager messageManager) {
		super(dm);
		this.messageManager = messageManager;

		// 排序
		this.setAutoCreateRowSorter(true);

		// 隐藏第一列(id)
		this.getColumnModel().getColumn(0).setMaxWidth(0);
		this.getColumnModel().getColumn(0).setMinWidth(0);
		this.getTableHeader().getColumnModel().getColumn(0).setMaxWidth(0);
		this.getTableHeader().getColumnModel().getColumn(0).setMinWidth(0);

		// TODO: caution!
		this.getColumnModel().getColumn(1).setMaxWidth(80);
		this.getColumnModel().getColumn(3).setMaxWidth(50);
		// GUI.FitTableColumns(this);

		this.setRowHeight(20);
		this.addMouseListener(new MouseAdapter() {

			@Override
			public void mouseClicked(MouseEvent e) {
				int row = rowAtPoint(e.getPoint());
				int col = columnAtPoint(e.getPoint());

				if (row < 0)
					return;
				Message message = MessageManager
						.getMessageById((Long) getValueAt(row, 0));
				if (col == 1 && message.hasAttach()) {
					Table_Msg.this.messageManager.downloadAttach(message);
				} else if (col == 2) {
					GUI.showModalDialog(new Dlg_Msg(
							Table_Msg.this.messageManager, message));

				} else if (col == 3) {
					String comment = JOptionPane.showInputDialog("请输入评论");
					if (comment == null)
						return;
					Table_Msg.this.messageManager.comment(message, comment);
					JOptionPane.showMessageDialog(null, "评论成功");
				}
			}

		});
	}

	@Override
	protected JTableHeader createDefaultTableHeader() {

		return new JTableHeader(columnModel) {
			public String getToolTipText(MouseEvent e) {
				String tip = null;
				java.awt.Point p = e.getPoint();
				int index = columnModel.getColumnIndexAtX(p.x);
				int realIndex = columnModel.getColumn(index).getModelIndex();
				if (realIndex == 3) {
					tip = "点击评论";
				}
				if (realIndex == 1) {
					tip = "点击下载附件";
				}
				return tip;
			}
		};
	}

	public JToolTip createToolTip() {
		MultiLineToolTip tip = new MultiLineToolTip();
		return tip;
	}

	@Override
	public String getToolTipText(MouseEvent event) {
		java.awt.Point p = event.getPoint();
		int rowIndex = rowAtPoint(p);
		int colIndex = columnAtPoint(p);
		int realColumnIndex = convertColumnIndexToModel(colIndex);
		StringBuilder tip = null;
		String tip2 = null;
		// System.out.println(rowIndex+" "+this.getRowCount());
		if (rowIndex < this.getRowCount() && rowIndex >= 0) {
			Message msg = MessageManager.getMessageById((Long) getValueAt(
					rowIndex, 0));
			tip2 = ("发布者:" + msg.getPublisher().getName() + "\n发布日期:" + 
					 Mis.formatCalendar(msg.getCreateDate()) +"\n内容:"
							+ msg.getContent()+ "\n----------------------\n评论:\n");
			for (edu.sysu.cshare.model.Comment cmt : msg.getComments()) {
				tip2 += (cmt.getCommenter().getName() + ":" + cmt.getContent() + "\n");
			}
		}
		return tip2.toString();
	}

}
